package sql;

import java.sql.ResultSet;
import java.sql.SQLException;

import beans.RoleItemBean;
import beans.RolesBean;

public class RolesDao {
	private DataAccess dataAccess = new DataAccess();

	public RolesBean getRoles() throws ClassNotFoundException, SQLException {
		RolesBean rolesBean = null;
		
		String query = "SELECT ID, NAME FROM ROLES ORDER BY NAME";
	
		ResultSet rset = dataAccess.getSelectResult(query);
		
		rolesBean = new RolesBean();
		while (rset.next()) {
			Integer storyPoints = rset.getInt("ID");
			if (rset.wasNull()) {
				storyPoints = null;
			}
	
			RoleItemBean roleItemBean = new RoleItemBean();
			roleItemBean.setId(storyPoints);
			roleItemBean.setName(rset.getString("NAME"));
				
			rolesBean.addRow(roleItemBean);
		}
		dataAccess.closeConnection();
		
		return rolesBean;		
	}
	
	public String getRoleName(int roleId) throws ClassNotFoundException, SQLException {
		String roleName = "";
		
		ResultSet rset = dataAccess.getSelectResult("SELECT NAME FROM ROLES"
				+ " WHERE ID = " + roleId);

		while (rset.next()) {
			roleName = rset.getString("NAME");
		}
		dataAccess.closeConnection();

		return roleName;
	}

}
